package com.lv.service.room.splash

import android.content.Context
import android.os.Parcelable
import androidx.annotation.Keep
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Database
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.Update
import com.lv.service.room.helper.InspectionConverter
import kotlinx.parcelize.Parcelize

/**
 * 作者： ygx
 * 创建日期：2022/7/14 9:07 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 *
 */
@Database(entities = [AdDataBean::class], version = 1, exportSchema = false)
abstract class AdSplashDatabase : RoomDatabase() {
    abstract val adSplashDao: AdSplashDao

    companion object {

        private const val YGX_DB_NAME = "syy_adSplash_db"

        @Volatile
        private var instance: AdSplashDatabase? = null

        @Synchronized
        fun getInstance(context: Context): AdSplashDatabase {
            return instance ?: Room.databaseBuilder(
                context,
                AdSplashDatabase::class.java,
                YGX_DB_NAME
            ).build().also { instance = it }
        }
    }

}

@TypeConverters(InspectionConverter::class)
@Parcelize
@Entity(tableName = "adSplash_db")
@Keep
data class AdDataBean(
    @PrimaryKey
    var _ids: Int,
    val id: Int,
    val image: String,
    val position: String,
    val created: Long,
    val ended: Long,
    val status: String,
    val url: String,
    val remarks: String,
    val province: String,
    val jump: String,
    val type: String,
    val tag: String,
    val startup: Int,
//    var bitmapBase64: List<String>,
) : Parcelable

//2、dao层的定义
@Dao
interface AdSplashDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertFindAdDataBean(adDataBean: AdDataBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertSuspendFindAdDataBean(adDataBean: AdDataBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertSuspendFindAdDataBean(addressBean: List<AdDataBean>)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun updateFindAdDataBean(adDataBean: AdDataBean)

    @Delete
    fun deleteFindAdDataBean(adDataBean: AdDataBean)

    @Query("delete from adSplash_db")
    fun deleteAllAdDataBean()

    @Query("delete from adSplash_db")
    suspend fun deleteSuspendAllAdDataBean()

    // = , in , like
    @Query("select * from adSplash_db where _ids=:id")
    fun queryLiveFindAdDataBean(id: Int = 0): LiveData<AdDataBean>

    @Query("select * from adSplash_db")
    fun queryLiveFindAllAdDataBean(): LiveData<List<AdDataBean>>

    @Query("select * from adSplash_db where _ids=:id")
    fun queryFindAdDataBean(id: Int = 0): AdDataBean?

    @Query("select * from adSplash_db")
    fun queryFindAllAdDataBean(): List<AdDataBean>?

    @Query("select * from adSplash_db where _ids =:id")
    suspend fun getFindAdDataBean(id: Int = 0): AdDataBean?

    @Query("select * from adSplash_db")
    suspend fun getAllAdDataBean(): List<AdDataBean>?
}